package com.jiang.专题.动态规划.Q213;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/02/25 17:51
 */
class Solution {
    public int rob(int[] nums) {
        int len = nums.length;
        if (len == 1) {
            return nums[0];
        } else if (len == 2) {
            return Math.max(nums[0], nums[1]);
        }
        return Math.max(rob1(nums, 0, len - 1), rob1(nums, 1, len));
    }

    public int rob1(int[] nums, int start, int end) {
        int dp0 = 0, dp1 = 0;
        for (int i = start; i < end; i++) {
            int newDp = Math.max(dp1, dp0 + nums[i]);
            dp0 = dp1;
            dp1 = newDp;
        }
        return dp1;
    }
}
